<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
        content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/src/css/main.css" media="all">
</head>

<body>
    <div id="layui-body-content" class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <form class="layui-form layui-form-pane">
                    <div class="gridAuto">
                        <div class="layui-form-item">
                            <label class="layui-form-label">用户名</label>
                            <div class="layui-input-block">
                                <div class="layui-input-wrap">
                                    <input type="text" name="username" id="username" placeholder="请输入用户名"
                                        lay-affix="clear" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">真实姓名</label>
                            <div class="layui-input-block">
                                <div class="layui-input-wrap">
                                    <input type="text" name="truename" id="truename" placeholder="请输入真实姓名"
                                        lay-affix="clear" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="layui-btn-container selectFormBox">

                        <button class="layui-btn layuiadmin-btn-user-list  layui-btn-sm" type="button"
                            lay-on="Yjsearch">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>查询
                        </button>
                        <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                            <i class="layui-icon layui-icon-fonts-clear"></i>清空
                        </button>
                        <button class="layui-btn layui-btn-sm layui-btn-normal" type="button" lay-on="Yjadd">
                            <i class="layui-icon layui-icon-add-circle layuiadmin-button-btn"></i>添加
                        </button>
                    </div>
                </form>
                <table id="LAY-app-user-list" lay-filter="LAY-app-user-list"></table>
                <script type="text/html" id="ID-table-status-switch">
                    <input type="checkbox" name="status" value="{{= d.id }}" title="启用|禁用" lay-skin="switch" lay-filter="demo-templet-status" {{= d.isenabled == '0' ? "checked" : "" }}>
                  </script>
                <script id="LAY-app-user-toolbar" type="text/html">
                    <div class="layui-clear-space">
                        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                        <a class="layui-btn layui-btn-xs  layui-bg-orange" lay-event="enabled">解锁</a>
                        <a class="layui-btn layui-btn-xs  " lay-event="reset">重置密码</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                    </div>
                </script>
            </div>
        </div>
    </div>

    <script src="/layui/layui.js"></script>
    <script>
        layui.config({
            base: '/src/'
        }).use('index', function () {
            var table = layui.table,
                $ = layui.$,
                $body = $('body'),
                main = layui.main,
                dropdown = layui.dropdown,
                form = layui.form,
                util = layui.util,
                layer = layui.layer,
                setter = layui.setter,
                admin = layui.admin;

            table.render({
                elem: '#LAY-app-user-list',
                url: admin.getUrl('sysuser/GetUserList'),
                // where: {
                //     pid: pid,
                // },
                cols: [[
                    {
                        type: 'numbers',
                        fixed: 'left',
                        title: '序号'
                    }
                    , {
                        field: 'username',
                        title: '用户名'
                    }, {
                        field: 'truename',
                        title: '真实姓名'
                    }, {
                        field: 'phone',
                        title: '联系电话'
                    }, {
                        field: 'emaile',
                        title: '邮箱'
                    }, {
                        field: 'user_type',
                        title: '用户类型', templet: function (d) {
                            if (d.user_type == 1) {
                                return "企业用户";
                            } else if (d.user_type == 2) {
                                return "监管用户";
                            } else {
                                return "";
                            }

                        }
                    }, {
                        field: 'jgjb',
                        title: '监管级别', templet: function (d) {
                            if (d.user_type == 1) {
                                return "省级";
                            } else if (d.user_type == 2) {
                                return "市级";
                            } else if (d.user_type == 3) {
                                return "县级";
                            } else if (d.user_type == 4) {
                                return "乡镇级";
                            } else {
                                return "";
                            }

                        }
                    }, {
                        title: '状态',
                        width: 100,
                        templet: '#ID-table-status-switch'
                    }
                    , {
                        title: '操作',
                        width: 250,
                        align: 'center',
                        fixed: 'right',
                        toolbar: '#LAY-app-user-toolbar'
                    }]
                ],
                height: 'full-200',
                page: true,
                limit: 30,
                done: function (res) {
                }
            });

            // 状态 - 开关操作
            form.on('switch(demo-templet-status)', function (obj) {
                var value = this.value;
                var id = value;
                var status = this.checked ? 0 : 1;
                admin.req({
                    url: 'sysuser/UnIsEnable',
                    data: JSON.stringify({
                        id: Number(id),
                        isenabled: Number(status)
                    }),
                    contentType: 'application/json', // 强制 JSON 格式
                    type: 'post',
                    done: function (res) {
                        if (res.successful) {
                            main.msg_success('修改成功！');
                        } else {
                            main.msg_error(res.msg);
                        }
                    }
                });
            });
            // 统一事件处理 (按钮)
            util.on({
                Yjsearch: function () {
                    var keywords = $("#username").val();
                    table.reload('LAY-app-user-list', {
                        where: {
                            username: keywords
                        },
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                    });
                },
                Yjadd: function () {
                    layer.open({
                        type: 2,
                        resize: false,
                        shadeClose: true,
                        area: ['70%', '90%'],
                        title: '添加用户',
                        content: setter.views + 'sys/user/yj_sys_users_add.html'

                    });
                }
            });

            // 触发表格单元格工具事件
            table.on('tool(LAY-app-user-list)', function (obj) { // 双击 toolDouble
                var data = obj.data; // 获得当前行数据
                if (obj.event === 'edit') {
                    layer.open({
                        title: '编辑 - ' + data.username,
                        type: 2,
                        area: ['70%', '90%'],
                        title: '编辑用户',
                        content: setter.views + 'sys/user/yj_sys_users_add.html?id=' + data.id,
                    });
                }
                if (obj.event === 'power') {
                    layer.open({
                        title: '角色管理 - ' + data.username,
                        type: 2,
                        area: ['70%', '90%'],
                        title: '编辑用户角色',
                        content: setter.views + 'sys/user/poweredit.html?id=' + data.id,
                    });
                }
                else if (obj.event == 'enabled') {
                    /*解除30分钟锁定上*/
                    var msg = "确定解锁吗？";
                    layer.confirm(msg, function (index) {
                        admin.req({
                            url: 'sysuser/UnlockLogin',
                            type: 'post',
                            data: JSON.stringify({
                                id: data.id
                            }),
                            contentType: 'application/json',
                            done: function (res) {
                                if (res.successful) {
                                    table.reload('LAY-app-user-list');

                                    admin.msg_success('解锁成功！');
                                }
                                else {
                                    admin.msg_error('删除失败！');
                                }
                            }
                        });
                        layer.close(index);
                    });
                }
                else if (obj.event == 'reset') {
                    /*重置密码*/
                    var msg = "确定重置密码吗？";
                    layer.confirm(msg, function (index) {
                        admin.req({
                            url: 'sysuser/ResetPwd',
                            type: 'post',
                            data: JSON.stringify({
                                id: data.id
                            }),
                            contentType: 'application/json',
                            done: function (res) {
                                if (res.successful) {
                                    table.reload('LAY-app-user-list');
                                    admin.msg_success('重置成功，新密码默认为123456aA@！');
                                }
                                else {
                                    admin.msg_error(res.msg);
                                }
                            }
                        });
                        layer.close(index);
                    });
                }
                else if (obj.event == 'del') {
                    var msg = "确定删除吗？";
                    layer.confirm(msg, function (index) {
                        admin.req({
                            url: 'sysuser/DeleteUser',
                            type: 'delete',
                            data: {
                                id: data.id
                            },
                            contentType: 'application/json',
                            done: function (res) {
                                if (res.successful) {
                                    table.reload('LAY-app-user-list');

                                    admin.msg_success('删除成功！');
                                }
                                else {
                                    admin.msg_error(res.msg);
                                }
                            }
                        });
                        layer.close(index);
                    });
                }
            });
            /* 导出弹出层 */
            $body.on("click", ".open-popup-export", function () {
                main.exportPopup();
            });

            /* 关闭弹出层 */
            $(".close-popup").click(function (e) {
                e.preventDefault();
                parent.layer.close(window.parent.layerIndex);
            });


        })
    </script>
</body>

</html>